package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author shiminghui
 * @date 2024/11/1 17:52
 * @description: TODO
 */
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmChannelMapper wmChannelMapper;
    @Autowired
    private WmNewsMapper wmNewsMapper;


    /**
     * 获取所有频道
     *
     * @return
     */
    @Override
    public ResponseResult channelList() {
        // 查询所有频道
        List<WmChannel> list = list();
        // 返回结果
        return ResponseResult.okResult(list);
    }

    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Long id) {

        WmChannel wmChannel = wmChannelMapper.selectById(id);
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (wmChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道已启用，不可删除");
        }
        int i = wmChannelMapper.deleteById(id);
        if (i <= 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "删除失败");
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmChannelDto dto) {

        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();

        Page<WmChannel> objectPage = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> queryWrapper = Wrappers.lambdaQuery(WmChannel.class);
        if (dto.getName() != null && !dto.getName().isEmpty()) {
            queryWrapper.like(WmChannel::getName, dto.getName());
        }
        queryWrapper.orderByDesc(WmChannel::getCreatedTime); // 按时间降序
        Page<WmChannel> resultPage = wmChannelMapper.selectPage(objectPage, queryWrapper);
        PageResponseResult result = new PageResponseResult();
        result.setData(resultPage.getRecords());
        result.setTotal((int) resultPage.getTotal());
        result.setCurrentPage(dto.getPage());
        result.setSize(dto.getSize());

        return result;
    }

    /**
     * 保存频道
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveWmChannel(WmChannel wmChannel) {
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmChannel wmChannel1 = wmChannelMapper.selectOne(Wrappers.lambdaQuery(WmChannel.class).eq(WmChannel::getName, wmChannel.getName()));
        if (wmChannel1 != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道已存在");
        }

        wmChannel.setStatus(false); // 未启用
        wmChannel.setCreatedTime(new Date()); // 创建时间

        int i = wmChannelMapper.insert(wmChannel);
        if (i <= 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "保存失败");
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 更新频道
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult update(WmChannel wmChannel) {

        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmChannel wmChannel1 = wmChannelMapper.selectById(wmChannel.getId());
        if (wmChannel1 == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        // 判断频道是否变为禁用
        if (wmChannel1.getStatus() && !wmChannel.getStatus()) {
            // 检查该频道下是否有文章
            Integer i = wmNewsMapper.selectCount(Wrappers.lambdaQuery(WmNews.class).eq(WmNews::getChannelId, wmChannel.getId()).eq(WmNews::getStatus, WmNews.Status.PUBLISHED.getCode()));
            if (i > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "该频道下有文章，不可禁用");
            }
        }

        int i = wmChannelMapper.updateById(wmChannel);
        if (i <= 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "更新失败");
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
